#coding:utf-8
__author__ = '自之梦'

"""
@author:自之梦
@time: 17/8/24 上午10:26
强生补货单接口测试
"""
import random
import time
import json

def Ma5_Val(str, val_type=""):
    #MD5加密
    import hashlib
    import types
    if type(str) is types.StringType:
        md5 = hashlib.md5()
        md5.update(str)
        if val_type == "upper":
            return md5.hexdigest().upper()
        else:
            return md5.hexdigest()
    else:
        return "errorCode: parameter1 must be str "

def Data_Key_Sort(data):
    #字典key排序
    result=sorted(data.items(), key=lambda item:item[0])
    test=""
    for i in result:
        test = test + "".join(i)
    return test

def Key_Val_Sort(data):
    #k1=v1&k2=v2&k3=v3
    result=sorted(data.items(), key=lambda item:item[0])
    test = []
    for i in result:
        result = "=".join(i)
        test.append(result)

    return "&".join(test)

def better_print(json_str):
        return json.dumps(json_str, indent=4)
    

if __name__ =="__main__":

    test='123456customerIdLJJ-Tformatjsonmethodshop.delivery.order.createsign_methodmd5timestamp2017-08-23 15:03:09v1.0\
{"shopCode": "XZQN", "takeTime": "2017-08-23 15:03:09", \
"orderLines": \
[{"barcode": "test_qs_same", "customBc": "qs_test_001", "quantity": "2"}\
],"totalCount":66, "categoryCount":1, "deliveryOrderCode": "BX001"}123456'
    
    result =Ma5_Val(test,"upper")
    #print result
    
    testdata = '{"shopCode": "XZQN", "takeTime": "2017-08-23 15:03:09", "orderLines": \
[{\
"barcode": "test_qs_same", "customBc": "qs_test_001", "quantity": "2"}\
],"totalCount":66, "categoryCount":1, "deliveryOrderCode": "BX001"}'
    import requests
    
    parms = {
        "format" : "json",
        "v" : "1.0",
        "method" : "shop.delivery.order.create",
        "timestamp" : "2017-08-23 15:03:09",
        "sign" : result,
        "sign_method" : "md5",
        "customerId" : "LJJ-T"
        
        }
    url = "http://121.14.47.186:7510/shopserv-portal/rest.htm?{0}".format(Key_Val_Sort(parms))    
    r = requests.post(url, data=testdata)
    print r.url
    print r.status_code,"\n"
    print "Response:\n",better_print(r.json())






